﻿package com.jacky.utils
{
	import com.rialife.as3.URL;
	import flash.external.ExternalInterface;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import nl.demonsters.debugger.MonsterDebugger;
	
	/**
	 * URL
	 * @author jacky
	 * @qq 416545903
	 * @msn pgq_012@163.com
	 */
	public class URL
	{
		
		private static var func_get_document_location:String = '_URL_getDocumentLocaiton_';
		private static var inited:Boolean = false;
		private static var _url:String;
		private static var _location:Object;
		
		public static function initialize():void
		{
			if ( !inited )
			{
				func_get_document_location += String(new Date().getTime());
				ExternalInterface.call('eval', 'function ' + func_get_document_location + '(){return window.location;}');
				inited = true;
			}
		}
		
		public static function getParamValue(s:String):String
		{
			!inited && initialize();
			
			if ( ExternalInterface.available )
			{
				getDocumentLocation();
				var parameters:String = _url.substr(_url.indexOf("?") + 1);
				var parameterItems:Array = parameters.split("&");
				var parameterName:String;
				var parameterVar:String;
				for (var i:* in parameterItems)
				{
					parameterName = parameterItems[i].split("=")[0];
					parameterVar = parameterItems[i].split("=")[1];
					if ( parameterName == s )
					return(parameterVar ? parameterVar : null);
				}
				return null;
			}
			return null;
		}
		
		public static function get hash():String 
		{ 
			getDocumentLocation();
			return String(_location) == '[object Object]' ? _location.hash : ''; 
		}
		
		public static function fresh():void
		{
			navigateToURL(new URLRequest('javascript:location.reload();'), '_self');
		}
		
		/*
		public static function open($request:LiteURLRequest):void
		{
			//if ( $request.track )
			$request.nocache = false;
			MonsterDebugger.trace(URL, $request);
			navigateToURL($request.request, $request.target);
		}
		*/
		
		static private function getDocumentLocation():void
		{
			!inited && initialize();
			_location = Object(ExternalInterface.call(func_get_document_location));
		}
		
		static public function get href():String 
		{ 
			getDocumentLocation();
			return _location.href; 
		}
	}
	
}
	class Enforce{}